import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _isEmpty from "lodash/isEmpty";
var _excluded = ["subMenus"];
import { getResponse, createPagination } from 'utils/utils';
import { queryMapIdpValue, queryIdpValue } from 'cpaas-front/lib/services/api';
import { queryTenant as _queryTenant, queryMenuTree, queryCode as _queryCode, queryDir, checkMenuDirExists as _checkMenuDirExists, queryPermissionSetTree as _queryPermissionSetTree, queryPermissionsByMenuIdAll, queryLovsByMenuIdAll, queryCopyMenuList as _queryCopyMenuList, copyMenu as _copyMenu, createMenu as _createMenu, saveMenu as _saveMenu, enable as _enable, disable as _disable, queryPermissionsByMenuId as _queryPermissionsByMenuId, queryLovByMenuId as _queryLovByMenuId, queryLabelList as _queryLabelList, queryMenuLabel as _queryMenuLabel } from "../services/tenantMenuManageService";

function defineProperty(obj, property, value) {
  Object.defineProperty(obj, property, {
    value: value,
    writable: true,
    enumerable: false,
    configurable: true
  });
}

export default {
  namespace: 'tenantMenuManage',
  state: {
    tenantData: {},
    // 租户数据
    pagination: {},
    // 分页参数
    code: {},
    // 值集集合
    list: {
      // 列表页面数据集合
      dataSource: [],
      // 表格数据
      pagination: {},
      // 分页配置
      flatKey: [] // 用于控制是否全部展开/收起的行数据key集合

    },
    menuPrefixList: [],
    // 菜单前缀
    menuTypeList: [],
    // 菜单类型
    customMenu: {
      list: []
    },
    customizeList: [],
    siteLabelList: [],
    tenantLabelList: []
  },
  effects: {
    // 获取初始化数据
    initCustomizeList: /*#__PURE__*/_regeneratorRuntime.mark(function initCustomizeList(_, _ref) {
      var call, put, customizeList;
      return _regeneratorRuntime.wrap(function initCustomizeList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.t0 = getResponse;
              _context.next = 4;
              return call(queryIdpValue, 'CPFM.FLAG');

            case 4:
              _context.t1 = _context.sent;
              customizeList = (0, _context.t0)(_context.t1);
              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  customizeList: customizeList
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, initCustomizeList);
    }),
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref2, _ref3) {
      var payload, put, call, lovCodes, code, _code$menuPrefix, menuPrefixList, _code$menuType, menuTypeList;

      return _regeneratorRuntime.wrap(function init$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              put = _ref3.put, call = _ref3.call;
              lovCodes = payload.lovCodes;
              _context2.t0 = getResponse;
              _context2.next = 6;
              return call(queryMapIdpValue, lovCodes);

            case 6:
              _context2.t1 = _context2.sent;
              code = (0, _context2.t0)(_context2.t1);

              if (!code) {
                _context2.next = 12;
                break;
              }

              _code$menuPrefix = code.menuPrefix, menuPrefixList = _code$menuPrefix === void 0 ? [] : _code$menuPrefix, _code$menuType = code.menuType, menuTypeList = _code$menuType === void 0 ? [] : _code$menuType;
              _context2.next = 12;
              return put({
                type: 'updateStateReducer',
                payload: {
                  menuPrefixList: menuPrefixList,
                  menuTypeList: menuTypeList
                }
              });

            case 12:
              return _context2.abrupt("return", code);

            case 13:
            case "end":
              return _context2.stop();
          }
        }
      }, init);
    }),
    queryLabelList: /*#__PURE__*/_regeneratorRuntime.mark(function queryLabelList(_, _ref4) {
      var put, call, res, res2;
      return _regeneratorRuntime.wrap(function queryLabelList$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              put = _ref4.put, call = _ref4.call;
              _context3.next = 3;
              return call(_queryLabelList, {
                level: 'SITE',
                type: 'MENU'
              });

            case 3:
              res = _context3.sent;
              _context3.next = 6;
              return call(_queryLabelList, {
                level: 'TENANT',
                type: 'MENU'
              });

            case 6:
              res2 = _context3.sent;
              _context3.next = 9;
              return put({
                type: 'updateStateReducer',
                payload: {
                  siteLabelList: res,
                  tenantLabelList: res2
                }
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, queryLabelList);
    }),
    queryMenuLabel: /*#__PURE__*/_regeneratorRuntime.mark(function queryMenuLabel(_ref5, _ref6) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function queryMenuLabel$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call;
              _context4.next = 4;
              return call(_queryMenuLabel, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", res);

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, queryMenuLabel);
    }),
    // 查询数据
    queryTenant: /*#__PURE__*/_regeneratorRuntime.mark(function queryTenant(_ref7, _ref8) {
      var payload, call, put, response, tenantData;
      return _regeneratorRuntime.wrap(function queryTenant$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call, put = _ref8.put;
              _context5.next = 4;
              return call(_queryTenant, payload);

            case 4:
              response = _context5.sent;
              tenantData = getResponse(response);

              if (!tenantData) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  tenantData: tenantData,
                  pagination: createPagination(tenantData)
                }
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, queryTenant);
    }),
    // 查询菜单列表树形数据
    queryTreeList: /*#__PURE__*/_regeneratorRuntime.mark(function queryTreeList(_ref9, _ref10) {
      var params, put, call, res, response, rowKeys, getDataSource, dataSource;
      return _regeneratorRuntime.wrap(function queryTreeList$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              getDataSource = function _getDataSource() {
                var collections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
                return collections.map(function (n) {
                  var m = _objectSpread({}, n);

                  if (!_isEmpty(m.subMenus)) {
                    rowKeys.push(n.id);
                    m.subMenus = getDataSource(m.subMenus);
                  } else {
                    m.subMenus = null;
                  }

                  return m;
                });
              };

              params = _ref9.params;
              put = _ref10.put, call = _ref10.call;
              _context6.next = 5;
              return call(queryMenuTree, _objectSpread({}, params));

            case 5:
              res = _context6.sent;
              response = getResponse(res);
              rowKeys = [];
              /**
               * 组装新dataSource
               * @function getDataSource
               * @param {!Array} [collections = []] - 树节点集合
               * @param {string} parentName - 上级目录名称
               * @returns {Array} - 新的dataSourcee
               */

              dataSource = getDataSource(response);

              if (!response) {
                _context6.next = 12;
                break;
              }

              _context6.next = 12;
              return put({
                type: 'updateListReducer',
                payload: {
                  dataSource: dataSource,
                  rowKeys: rowKeys
                }
              });

            case 12:
              return _context6.abrupt("return", response);

            case 13:
            case "end":
              return _context6.stop();
          }
        }
      }, queryTreeList);
    }),
    // 查询值集
    queryCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryCode(_ref11, _ref12) {
      var payload, put, call, response;
      return _regeneratorRuntime.wrap(function queryCode$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref11.payload;
              put = _ref12.put, call = _ref12.call;
              _context7.next = 4;
              return call(_queryCode, payload);

            case 4:
              response = _context7.sent;

              if (!(response && !response.failed)) {
                _context7.next = 8;
                break;
              }

              _context7.next = 8;
              return put({
                type: 'setCodeReducer',
                payload: _defineProperty({}, payload.lovCode, response)
              });

            case 8:
            case "end":
              return _context7.stop();
          }
        }
      }, queryCode);
    }),
    // 查询上级目录
    queryParentDir: /*#__PURE__*/_regeneratorRuntime.mark(function queryParentDir(_ref13, _ref14) {
      var params, call, res, response;
      return _regeneratorRuntime.wrap(function queryParentDir$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              params = _ref13.params;
              call = _ref14.call;
              _context8.next = 4;
              return call(queryDir, params);

            case 4:
              res = _context8.sent;
              response = getResponse(res);
              return _context8.abrupt("return", {
                dataSource: response ? response.content : [],
                pagination: response ? createPagination(response) : {}
              });

            case 7:
            case "end":
              return _context8.stop();
          }
        }
      }, queryParentDir);
    }),
    // 检查目录编码是否存在
    checkMenuDirExists: /*#__PURE__*/_regeneratorRuntime.mark(function checkMenuDirExists(_ref15, _ref16) {
      var params, call, res;
      return _regeneratorRuntime.wrap(function checkMenuDirExists$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              params = _ref15.params;
              call = _ref16.call;
              _context9.next = 4;
              return call(_checkMenuDirExists, params);

            case 4:
              res = _context9.sent;
              return _context9.abrupt("return", res);

            case 6:
            case "end":
              return _context9.stop();
          }
        }
      }, checkMenuDirExists);
    }),
    // 查询菜单下已分解的权限集树
    queryPermissionSetTree: /*#__PURE__*/_regeneratorRuntime.mark(function queryPermissionSetTree(_ref17, _ref18) {
      var menuId, params, call, res, response, defaultExpandedRowKeys, getDataSource, dataSource;
      return _regeneratorRuntime.wrap(function queryPermissionSetTree$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              getDataSource = function _getDataSource2() {
                var collections = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
                var parentId = arguments.length > 1 ? arguments[1] : undefined;
                var keyPath = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
                return collections.map(function (n) {
                  var m = _objectSpread(_objectSpread({}, n), {}, {
                    key: n.id
                  });

                  if (_isEmpty(m.keyPath)) {
                    defineProperty(m, 'keyPath', _isEmpty(keyPath) ? [] : keyPath);
                  }

                  m.keyPath = keyPath.concat(parentId);

                  if (!_isEmpty(m.subMenus)) {
                    m.subMenus = getDataSource(m.subMenus, m.id, m.keyPath);
                    defaultExpandedRowKeys.push(m.id);
                  } else {
                    m.subMenus = null;
                  }

                  return m;
                });
              };

              menuId = _ref17.menuId, params = _ref17.params;
              call = _ref18.call;
              _context10.next = 5;
              return call(_queryPermissionSetTree, menuId, params);

            case 5:
              res = _context10.sent;
              response = getResponse(res);
              defaultExpandedRowKeys = [];
              /**
               * 组装新dataSource
               * @function getDataSource
               * @param {!Array} [collections = []] - 树节点集合
               * @param {string} parentName - 上级目录名称
               * @returns {Array} - 新的dataSourcee
               */

              dataSource = getDataSource(response || []);
              return _context10.abrupt("return", {
                dataSource: dataSource,
                defaultExpandedRowKeys: defaultExpandedRowKeys
              });

            case 10:
            case "end":
              return _context10.stop();
          }
        }
      }, queryPermissionSetTree);
    }),
    // 查询权限集下已分配的权限
    queryPermissionsByIdAll: /*#__PURE__*/_regeneratorRuntime.mark(function queryPermissionsByIdAll(_ref19, _ref20) {
      var permissionSetId, params, call, res, response;
      return _regeneratorRuntime.wrap(function queryPermissionsByIdAll$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              permissionSetId = _ref19.permissionSetId, params = _ref19.params;
              call = _ref20.call;
              _context11.next = 4;
              return call(queryPermissionsByMenuIdAll, permissionSetId, params);

            case 4:
              res = _context11.sent;
              response = getResponse(res);
              return _context11.abrupt("return", {
                dataSource: response ? response.content : [],
                pagination: response ? createPagination(response) : {}
              });

            case 7:
            case "end":
              return _context11.stop();
          }
        }
      }, queryPermissionsByIdAll);
    }),
    // 查询权限集下已分配的Lov
    queryLovsByIdAll: /*#__PURE__*/_regeneratorRuntime.mark(function queryLovsByIdAll(_ref21, _ref22) {
      var permissionSetId, params, call, res, response;
      return _regeneratorRuntime.wrap(function queryLovsByIdAll$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              permissionSetId = _ref21.permissionSetId, params = _ref21.params;
              call = _ref22.call;
              _context12.next = 4;
              return call(queryLovsByMenuIdAll, permissionSetId, params);

            case 4:
              res = _context12.sent;
              response = getResponse(res);
              return _context12.abrupt("return", {
                dataSource: response ? response.content : [],
                pagination: response ? createPagination(response) : {}
              });

            case 7:
            case "end":
              return _context12.stop();
          }
        }
      }, queryLovsByIdAll);
    }),
    // 查询当前复制的节点及其子节点
    queryCopyMenuList: /*#__PURE__*/_regeneratorRuntime.mark(function queryCopyMenuList(_ref23, _ref24) {
      var params, call, res, response, defaultExpandedRowKeys, defaultSelectedRows, traverseTree, copyMenuListDefaultRows;
      return _regeneratorRuntime.wrap(function queryCopyMenuList$(_context13) {
        while (1) {
          switch (_context13.prev = _context13.next) {
            case 0:
              traverseTree = function _traverseTree(node) {
                if (!node) {
                  return;
                }

                defaultExpandedRowKeys.push(node.id);
                defaultSelectedRows.push(node);

                if (node.subMenus && node.subMenus.length > 0) {
                  for (var i = 0; i < node.subMenus.length; i++) {
                    traverseTree(node.subMenus[i]);
                  }
                }
              };

              params = _ref23.params;
              call = _ref24.call;
              _context13.next = 5;
              return call(_queryCopyMenuList, params);

            case 5:
              res = _context13.sent;
              response = getResponse(res);
              defaultExpandedRowKeys = [];
              defaultSelectedRows = [];

              if (!response) {
                _context13.next = 13;
                break;
              }

              response.forEach(function (item) {
                return traverseTree(item);
              });
              copyMenuListDefaultRows = defaultSelectedRows.map(function (row) {
                var nextRow = _objectSpread({}, row);

                var subMenus = nextRow.subMenus,
                    rest = _objectWithoutProperties(nextRow, _excluded);

                var newValue = _objectSpread({}, rest);

                return newValue;
              });
              return _context13.abrupt("return", {
                copyMenuList: response || [],
                copyMenuListDefaultRowKeys: defaultExpandedRowKeys || [],
                copyMenuListDefaultRows: copyMenuListDefaultRows || []
              });

            case 13:
            case "end":
              return _context13.stop();
          }
        }
      }, queryCopyMenuList);
    }),
    // 复制并创建菜单
    copyMenu: /*#__PURE__*/_regeneratorRuntime.mark(function copyMenu(_ref25, _ref26) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function copyMenu$(_context14) {
        while (1) {
          switch (_context14.prev = _context14.next) {
            case 0:
              payload = _ref25.payload;
              call = _ref26.call;
              _context14.next = 4;
              return call(_copyMenu, payload);

            case 4:
              res = _context14.sent;
              return _context14.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context14.stop();
          }
        }
      }, copyMenu);
    }),
    // 新建目录
    createMenu: /*#__PURE__*/_regeneratorRuntime.mark(function createMenu(_ref27, _ref28) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function createMenu$(_context15) {
        while (1) {
          switch (_context15.prev = _context15.next) {
            case 0:
              payload = _ref27.payload;
              call = _ref28.call;
              _context15.next = 4;
              return call(_createMenu, payload);

            case 4:
              res = _context15.sent;
              return _context15.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context15.stop();
          }
        }
      }, createMenu);
    }),
    // 更新目录
    saveMenu: /*#__PURE__*/_regeneratorRuntime.mark(function saveMenu(_ref29, _ref30) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function saveMenu$(_context16) {
        while (1) {
          switch (_context16.prev = _context16.next) {
            case 0:
              payload = _ref29.payload;
              call = _ref30.call;
              _context16.next = 4;
              return call(_saveMenu, payload);

            case 4:
              res = _context16.sent;
              return _context16.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context16.stop();
          }
        }
      }, saveMenu);
    }),
    enable: /*#__PURE__*/_regeneratorRuntime.mark(function enable(_ref31, _ref32) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function enable$(_context17) {
        while (1) {
          switch (_context17.prev = _context17.next) {
            case 0:
              payload = _ref31.payload;
              call = _ref32.call;
              _context17.next = 4;
              return call(_enable, payload);

            case 4:
              res = _context17.sent;
              return _context17.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context17.stop();
          }
        }
      }, enable);
    }),
    disable: /*#__PURE__*/_regeneratorRuntime.mark(function disable(_ref33, _ref34) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function disable$(_context18) {
        while (1) {
          switch (_context18.prev = _context18.next) {
            case 0:
              payload = _ref33.payload;
              call = _ref34.call;
              _context18.next = 4;
              return call(_disable, payload);

            case 4:
              res = _context18.sent;
              return _context18.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context18.stop();
          }
        }
      }, disable);
    }),
    // 查询权限集下可分配的所有权限
    queryPermissionsByMenuId: /*#__PURE__*/_regeneratorRuntime.mark(function queryPermissionsByMenuId(_ref35, _ref36) {
      var menuId, params, call, res, response;
      return _regeneratorRuntime.wrap(function queryPermissionsByMenuId$(_context19) {
        while (1) {
          switch (_context19.prev = _context19.next) {
            case 0:
              menuId = _ref35.menuId, params = _ref35.params;
              call = _ref36.call;
              _context19.next = 4;
              return call(_queryPermissionsByMenuId, menuId, params);

            case 4:
              res = _context19.sent;
              response = getResponse(res);
              return _context19.abrupt("return", {
                dataSource: response ? response.content : [],
                pagination: response ? createPagination(response) : {}
              });

            case 7:
            case "end":
              return _context19.stop();
          }
        }
      }, queryPermissionsByMenuId);
    }),
    queryLovByMenuId: /*#__PURE__*/_regeneratorRuntime.mark(function queryLovByMenuId(_ref37, _ref38) {
      var menuId, params, call, res, response;
      return _regeneratorRuntime.wrap(function queryLovByMenuId$(_context20) {
        while (1) {
          switch (_context20.prev = _context20.next) {
            case 0:
              menuId = _ref37.menuId, params = _ref37.params;
              call = _ref38.call;
              _context20.next = 4;
              return call(_queryLovByMenuId, menuId, params);

            case 4:
              res = _context20.sent;
              response = getResponse(res);
              return _context20.abrupt("return", {
                dataSource: response ? response.content : [],
                pagination: response ? createPagination(response) : {}
              });

            case 7:
            case "end":
              return _context20.stop();
          }
        }
      }, queryLovByMenuId);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    },
    setCodeReducer: function setCodeReducer(state, _ref39) {
      var payload = _ref39.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        code: Object.assign(state.code, payload)
      });
    },
    updateStateReducer: function updateStateReducer(state, _ref40) {
      var payload = _ref40.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updateListReducer: function updateListReducer(state, _ref41) {
      var payload = _ref41.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        list: _objectSpread(_objectSpread({}, state.list), payload)
      });
    }
  }
};